package at.hockey.ticker;

import java.util.ListIterator;

import at.hockey.ticker.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ViewFlipper;


public class HockeyTickerUI extends Activity implements OnTouchListener {
	
	private GestureDetector mGestureDetector; 
	private ViewFlipper mVF;
		
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set main xml layout
        setContentView(R.layout.main);
        
        this.mVF = (ViewFlipper) this.findViewById(R.id.main_vf);

        //test
		//TODO generate data objects with real data
		generateTestData();
		
		//initially build ui hierarchy
        this.mGestureDetector = new GestureDetector(new OnFlingGestureListener(this));
		
		if(MatchOverview.getInstance() != null){	// TODO
		
			this.mVF.addView(new MatchOverviewUI(this));
			this.mVF.getChildAt(0).findViewById(R.id.mo_details_sv).setOnTouchListener(this);
			// TODO listener auf VF?
			
	        ListIterator<MatchDetails> itr = MatchOverview.getInstance().getMatchDetails().listIterator();
		    while(itr.hasNext()){
		    	
		    	MatchDetailsUI mdui = new MatchDetailsUI(this, itr.next());
		    	this.mVF.addView(mdui);
		    	mdui.findViewById(R.id.md_entries_sv).setOnTouchListener(this);
		    	// TODO listener auf VF?
		    }

	    }
        setAppTitle();
    }
    
	@Override
	public boolean onTouch(View v, MotionEvent e) {

		return this.mGestureDetector.onTouchEvent(e);
	}
	
	public void setAppTitle(){
		if(this.mVF.getDisplayedChild() == 0){
			this.setTitle("HT > Matches");
		}
		else{
			MatchDetailsUI mdui = (MatchDetailsUI) this.mVF.getChildAt(this.mVF.getDisplayedChild());
			MatchDetails md = mdui.getMatchDetails(); 
			String matchup = md.getTeam1() + " " + md.getScore() + " " + md.getTeam2();

			this.setTitle("HT > Match " + this.mVF.getDisplayedChild() + "/" + (this.mVF.getChildCount() - 1) + " > " + matchup);						
		}
	}
	
	//test
	private void generateTestData() {
	
		MatchOverview mo = new MatchOverview();
    
	    MatchDetails md = new MatchDetails("30.03.2012", "RBS", "FAV", "Period 2", "Last Action RBS: GK ON", "Last Action FAV: 2 Min.");
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "RBS", "GK ON", "00:00", "38 - DIVIS Reinhard"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "FAV", "GK ON", "00:00", "01 - PINTARIC Matija"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "FAV", "2 Min.", "06:29 - 08:29", "4 - CVETEK Igor (Hooking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "RBS", "EQ", "1:0", "36 - PEWAL Marco"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "RBS", "EQ", "2:0", "89 - BRUCKER Marco"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "RBS", "2 Min.", "32:09 - 34:09", "18 - BOIS Daniel (Roughing)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "FAV", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    
	    mo.addMatchDetails(md);
	    
	    md = new MatchDetails("30.03.2012", "XXX", "YYY", "Period 1", "Last Action XXX: 2 Min.", "Last Action YYY: 0:1");
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "XXX", "GK ON", "00:00", "38 - DIVIS Reinhard"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "XXX", "2 Min.", "06:29 - 08:29", "4 - CVETEK Igor (Hooking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "YYY", "EQ", "2:0", "89 - BRUCKER Marco"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "XXX", "EQ", "2:1", "18 - BOIS Daniel (Roughing)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(3, "XXX", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(3, "LZG", "2 Min.", "37:28 - 39:28", "Lany roxx!"));

	    mo.addMatchDetails(md); 

	    md = new MatchDetails("01.04.2012", "RBS", "XXX", "Period 1", "Last Action XXX: 2 Min.", "Last Action YYY: 0:1");
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "RBS", "GK ON", "00:00", "38 - DIVIS Reinhard"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(1, "XXX", "GK ON", "00:00", "38 - DIVIS Reinhard"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "RBS", "EQ", "1:0", "89 - BRUCKER Marco"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "RBS", "EQ", "2:0", "18 - BOIS Daniel (Roughing)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(2, "RBS", "EQ", "3:0", "18 - BOIS Daniel (Roughing)"));
	    md.addMatchDetailsEntry(new MatchDetailsEntry(3, "XXX", "2 Min.", "37:28 - 39:28", "23 - DERVARIC Damjan (High Sticking)"));
	    
	    mo.addMatchDetails(md); 
	}

}
